#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Run a simple backup of the Bacula build directory 
#  to two tapes where the maximum tape file size is set to 1M
#  Note, this test simulates the tape filling and writing to
#   the next tape. One minor wrinkle: we create the two tapes
#   in the special ScratchPool defined in pool, so they should
#   be switched to the Special
#   Pool automatically.
#
#  Note we use the viritual disk autochanger
#
TestName="scratchpool-pool-test"
JobName=scratchpool-pool
. scripts/functions

# This test hangs so disable it
#exit

cwd=`pwd`
scripts/cleanup
scripts/copy-2disk-confs
scripts/prepare-disk-changer
 
echo "${cwd}/build" >${cwd}/tmp/file-list

outf="${cwd}/tmp/sed_tmp"
echo "s%# Maximum File Size%  Maximum File Size%g" >${outf}
cp ${cwd}/bin/bacula-sd.conf ${cwd}/tmp/1
sed -f ${outf} ${cwd}/tmp/1 >${cwd}/bin/bacula-sd.conf

change_jobname NightlySave $JobName
start_test

# Write out bconsole commands
cat <<END_OF_DATA >${cwd}/tmp/bconcmds
@output /dev/null
messages
@$out ${cwd}/tmp/log1.out
label storage=tape volume=TestVolume001 slot=1 pool=SpecialScratch drive=0
label storage=tape volume=TestVolume002 slot=2 pool=SpecialScratch drive=0
@#label storage=tape volume=TestVolume003 slot=3 pool=SpecialScratch drive=0
label storage=tape volume=TestVolume004 slot=4 pool=Scratch drive=0
show pool=Default
show pool=Special
@########################
@# Verify catalog entries
@$out ${cwd}/tmp/pool.out
sql
SELECT Name, ScratchPoolId, RecyclePoolId FROM Pool WHERE Name IN ('Default', 'Special');

@$out ${cwd}/tmp/log1.out
@########################
@# Use SpecialScratch
run job=$JobName pool=Special yes
wait
messages
run job=$JobName pool=Special level=Full yes
wait
messages
@$out ${cwd}/tmp/log3.out
@########################
@# no more volume in SpecialScratch (but Scrach have one)
run job=$JobName pool=Special level=Full yes
@# Wait mount seems to be broken so use sleep instead
@#wait mount
@sleep 10
@# At this point jobid=3 should be blocked waiting for 
@#  the sysop to create a new Volume, so cancel the job
cancel jobid=3
wait
messages
@$out ${cwd}/tmp/log4.out
list volume pool=Special
@# 
@# now do a restore
@#
@$out ${cwd}/tmp/log2.out
restore where=${cwd}/tmp/bacula-restores select all storage=tape done
yes
wait
messages
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=tape
stop_bacula

check_two_logs
check_restore_diff


################################################################
# Check Catalog definition for ScratchPool and RecyclePool
awk '
 / Default/ { if ($4 == 0 && $6 != 0) {print "OK"} else { print "Default ERR"} }
 / Special/ { if ($4 != 0 && $6 == 0) {print "OK"} else { print "Special ERR"} }
' tmp/pool.out > tmp/res

grep ERR tmp/res
if [ $? -eq 0 ]; then
    print_debug "Found ERR about ScratchPoolId/RecyclePoolId in catalog"
    dstat=2
fi

if [ `cat tmp/res | wc -l` -ne 2 ]; then
    print_debug "Can't find all pool records in catalog"
    dstat=2
fi

################################################################
# Check for the third job that must fail because the SpecialScratch is empty

grep 'Cannot find any appendable volumes' tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "Job 3 should request a manual intervention!!"
    dstat=2
fi

grep 'Backup Canceled' tmp/log3.out > /dev/null
if [ $? -ne 0 ]; then
    print_debug "Job 3 should be in error"
    dstat=2
fi

################################################################
# Check Special pool

if [ `grep TestVolume00 tmp/log4.out | wc -l` -ne 2 ]; then
    print_debug "Special pool should contain 2 vols"
    dstat=2
fi

end_test
